package app.ui;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.ToggleButton;
import database.local.Business;
import database.local.QueryData;
import database.local.Utils;

public class AddNewBusiness extends Activity {
	private String chosenDeal, businessName, addressString, comment;
	private Business curBusiness;
	private EditText businessText;
	final int  START_TIME_DIALOG_ID = 0, ENDS_TIME_DIALOG_ID = 1;
	private int startHour, startMinute, endHour, endMinute, rate = -1, money =-1;
	private Button startTimeButton, endTimeButton;
	private RatingBar ratingBar;
	private RatingBar moneyBar;
	private EditText commentEditText;
	private int dayBitmap = 0;
	private TimePicker startTimePicker, endTimePicker;
	
	
	OnTimeChangedListener startTimeSetListenr = new OnTimeChangedListener() {

		public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
			startHour = hourOfDay;
			startMinute = 0;			
		}
	};

	OnTimeChangedListener endTimeSetListenr = new OnTimeChangedListener() {

		public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
			endHour = hourOfDay;
			endMinute = minute;
		}	
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_new_business_layout);
		chosenDeal = "pub";
		
		//Deal type Spinner
		
		Spinner dealSpinner = (Spinner) findViewById(R.id.add_layout_type_spinner);
		dealSpinner.setOnItemSelectedListener( new DealOnItemSelectedListener());
		
		//Day Buttons
		final ToggleButton dayBtn_s = (ToggleButton) findViewById(R.id.add_layout_day_s_btn);
		dayBtn_s.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked){
					dayBitmap = dayBitmap | QueryData.SUNDAY;
				}
				else
				{
					dayBitmap = dayBitmap & ~QueryData.SUNDAY;
				}
			}
		});
		final ToggleButton dayBtn_m = (ToggleButton) findViewById(R.id.add_layout_day_m_btn);
		dayBtn_m.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked){
					dayBitmap = dayBitmap | QueryData.MONDAY;
				}
				else
				{
					dayBitmap = dayBitmap & ~QueryData.MONDAY;
				}
			}
		});
		final ToggleButton dayBtn_tu = (ToggleButton) findViewById(R.id.add_layout_day_tu_btn);
		dayBtn_tu.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked){
					dayBitmap = dayBitmap | QueryData.TUESDAY;
				}
				else
				{
					dayBitmap = dayBitmap & ~QueryData.TUESDAY;
				}
			}
		});
		final ToggleButton dayBtn_w = (ToggleButton) findViewById(R.id.add_layout_day_w_btn);
		dayBtn_w.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked){
					dayBitmap = dayBitmap | QueryData.WEDNESDAY;
				}
				else
				{
					dayBitmap = dayBitmap & ~QueryData.WEDNESDAY;
				}
			}
		});
		final ToggleButton dayBtn_th = (ToggleButton) findViewById(R.id.add_layout_day_th_btn);
		dayBtn_th.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked){
					dayBitmap = dayBitmap | QueryData.THURSDAY;
				}
				else
				{
					dayBitmap = dayBitmap & ~QueryData.THURSDAY;
				}
			}
		});
		final ToggleButton dayBtn_f = (ToggleButton) findViewById(R.id.add_layout_day_f_btn);
		dayBtn_f.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked){
					dayBitmap = dayBitmap | QueryData.FRIDAY;
				}
				else
				{
					dayBitmap = dayBitmap & ~QueryData.FRIDAY;
				}
			}
		});
		final ToggleButton dayBtn_sat = (ToggleButton) findViewById(R.id.add_layout_day_sat_btn);
		dayBtn_sat.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked){
					dayBitmap = dayBitmap | QueryData.SATURDAY;
				}
				else
				{
					dayBitmap = dayBitmap & ~QueryData.SATURDAY;
				}
			}
		});
		
		ToggleButton dayBtn_all = (ToggleButton) findViewById(R.id.add_layout_day_all_btn);
		dayBtn_all.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked){
					dayBitmap = dayBitmap | QueryData.ALL_DAYS;
					dayBtn_s.setChecked(true);
					dayBtn_m.setChecked(true);
					dayBtn_tu.setChecked(true);
					dayBtn_w.setChecked(true);
					dayBtn_th.setChecked(true);
					dayBtn_f.setChecked(true);
					dayBtn_sat.setChecked(true);					
				}
				else
				{
					dayBitmap = dayBitmap & ~QueryData.ALL_DAYS;
					dayBtn_s.setChecked(false);
					dayBtn_m.setChecked(false);
					dayBtn_tu.setChecked(false);
					dayBtn_w.setChecked(false);
					dayBtn_th.setChecked(false);
					dayBtn_f.setChecked(false);
					dayBtn_sat.setChecked(false);
				}
			}
		});
		
		 // Business name
		 businessText = (EditText) findViewById(R.id.add_layout_business_name_edittext);
		 businessText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_GO){
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(businessText.getWindowToken(), 0);
				return true;
				}
				return false;
			}
		});
		 
		 
		 // Comment
		 commentEditText = (EditText) findViewById(R.id.add_layout_edit_edittext);
		 
		 
		 
		 // Here we check if business already exist or create new business		 
		 if (!this.getIntent().getExtras().getBoolean("CreateNewBusiness")){
			 curBusiness = this.getIntent().getExtras().getParcelable("business");
			 businessText.setText(curBusiness.getName());
			 addressString = curBusiness.getStreet();
		 }
		 else
		 {
		//Dialog for business Name		
//			AlertDialog.Builder businessNameDialog = new AlertDialog.Builder(this);
//	
//			businessNameDialog.setTitle("New business name:");		
//			// Set an EditText view to get user input 
//			final EditText inputBusiness = new EditText(this);
//			
//			businessNameDialog.setView(inputBusiness);
//	
//			businessNameDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
//			public void onClick(DialogInterface dialog, int whichButton) {
//			  businessName = inputBusiness.getText().toString();
//			  updateBusinessText();
//			  }
//			});
//			businessNameDialog.show();

		 }

		 
	   
		
		//Initiate Time
		 final Calendar c = Calendar.getInstance();
	        startHour = c.get(Calendar.HOUR_OF_DAY);
	        startMinute = 0;
	        endHour = startHour + 2 <= 23 ? startHour + +2 : startHour + 2 - 24   ;
	        endMinute = 0;

		

						

		
		//Rating bar
		ratingBar = (RatingBar) findViewById(R.id.add_layout_rate_ratingbar);
		ratingBar.setRating(1);
		
		moneyBar = (RatingBar) findViewById(R.id.add_layout_money_ratingbar);
		moneyBar.setRating(1);		
		
		//Add button
				Button addButton = (Button) findViewById(R.id.add_layout_add_button);
				addButton.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {	
						businessName = businessText.getText().toString();
						comment = commentEditText.getText().toString();
						rate = (int) ratingBar.getRating();
						money = (int) moneyBar.getRating();
						NewDealData ndd = new NewDealData(startHour, startMinute, endHour, endMinute, chosenDeal, dayBitmap, businessName, rate, money, comment);
						Intent intent = new Intent();
						intent.putExtra("data", ndd);
						setResult(RESULT_OK, intent);
						finish();
					}
				});
		 
		//Cancel button
//		Button cancelButton = (Button) findViewById(R.id.add_layout_cancel_button);
//		cancelButton.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				Intent intent = new Intent();
//				setResult(RESULT_CANCELED, intent);
//				finish();
//			}
//		});
		
		startTimePicker = (TimePicker) findViewById(R.id.add_layout_start_timepicker);
		startTimePicker.setIs24HourView(true);
		startTimePicker.setOnTimeChangedListener(startTimeSetListenr);
		startTimePicker.setCurrentHour(startHour);
		startTimePicker.setCurrentMinute(startMinute);
		
		endTimePicker = (TimePicker) findViewById(R.id.add_layout_end_timepicker);
		endTimePicker.setIs24HourView(true);
		endTimePicker.setCurrentHour(endHour);
		endTimePicker.setCurrentMinute(endMinute);
		
		endTimePicker.setOnTimeChangedListener(endTimeSetListenr);
		
	}
	
	/*-------------------------------Updaters-----------------------------------------------------------------------*/
	protected void updateStartButton() {
		startTimeButton.setText(Utils.pad(startHour) + ":" + Utils.pad(startMinute));
	}
	
	protected void updateEndButton() {
		endTimeButton.setText(Utils.pad(endHour) + ":" + Utils.pad(endMinute));
	}



	protected void updateBusinessText() {
		businessText.setText(businessName);
	}
	
	
	/*-------------------------------Spinners Listeners ----------------------------------------------------------*/

	public class DealOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView<?> parent,
	        View view, int pos, long id) {
	    	String temp = parent.getItemAtPosition(pos).toString();
	    	if (temp.equalsIgnoreCase("pub"))
	    	{
	    	chosenDeal = "pub";
	    	}
	    	else if (temp.equalsIgnoreCase("restaurant"))
	    	{
	    	chosenDeal = "rest";
	    	}
	    	else if (temp.equalsIgnoreCase("party"))
	    	{
	    	chosenDeal = "party";
	    	}
	    }

	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	}
	


	
}
